package cn.imnaer.service.rpc.userinfo.dao;

import cn.imnaer.database.BaseDao;
import cn.imnaer.database.MatchConst;
import cn.imnaer.database.define.DBConstant;
import cn.imnaer.service.persistence.model.FriendRequest;
import cn.imnaer.service.persistence.model.consts.FRIEND_REQUEST;

import org.bson.BSONObject;
import org.bson.BasicBSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/5/27.
 */
public class FriendRequestDaoImpl extends BaseDao<FriendRequest> {

    private static final FriendRequestDaoImpl FRIEND_REQUEST_ACTIVE_RECORD = new FriendRequestDaoImpl();

    private FriendRequestDaoImpl() {
        this.dbSpaceName = DBConstant.SPACE.USER_INFO_SPACE;
        this.dbCollectionName = DBConstant.COLLECTION.NEW_FRIEND_REQUEST;
        this.primaryKey = FRIEND_REQUEST.friendRequestId;
    }

    public static final FriendRequestDaoImpl getInstance() {
        return FriendRequestDaoImpl.FRIEND_REQUEST_ACTIVE_RECORD;
    }

    /**
     * 根据发送人和接受人获得好友请求信息
     *
     * @param sendUserId
     * @param acceptUserId
     * @return
     */
    public FriendRequest findBySendUserAndAcceptUser(String sendUserId, String acceptUserId) {
        FriendRequest target = null;
        BasicBSONObject query = new BasicBSONObject();
        query.put(FRIEND_REQUEST.sendUserId, sendUserId);
        query.put(FRIEND_REQUEST.acceptUserId, acceptUserId);
        return  this.findOne(query);
 
  
    }

    /**
     * 根据接收人获得好友请求
     *
     * @return
     */
    public List<FriendRequest> findByAcceptUser(String userId) {
        BasicBSONObject query = new BasicBSONObject();
        query.put(FRIEND_REQUEST.acceptUserId, userId);
        BasicBSONObject order = new BasicBSONObject();
        order.put(FRIEND_REQUEST.createTime, -1);
        List<FriendRequest> records = this.findAll(query, order);
        return records;
    }

    /**
     * 根据发送人获得好友请求
     *
     * @return
     */
    public List<FriendRequest> findBySendUser(String userId) {
        BasicBSONObject query = new BasicBSONObject();
        query.put(FRIEND_REQUEST.sendUserId, userId);
        BasicBSONObject order = new BasicBSONObject();
        order.put(FRIEND_REQUEST.createTime, -1);
        List<FriendRequest> records = this.findAll(query, order);
        return records;
    }

    public Long countUnReadByAcceptUser(String userId) {
        BasicBSONObject query = new BasicBSONObject();
        query.put(FRIEND_REQUEST.acceptUserId, userId);
        query.put(FRIEND_REQUEST.isUnReadForAcceptUser, true);
        Long unReadCount = this.count(query);
        return unReadCount;
    }

    public Long countUnReadBySendUser(String userId) {
        BasicBSONObject query = new BasicBSONObject();
        query.put(FRIEND_REQUEST.sendUserId, userId);
        query.put(FRIEND_REQUEST.isUnReadForSendUser, true);
        Long unReadCount = this.count(query);
        return unReadCount;
    }

    public List<FriendRequest> findUnReadByAcceptUser(String userId) {
        BasicBSONObject query = new BasicBSONObject();
        query.put(FRIEND_REQUEST.acceptUserId, userId);
        query.put(FRIEND_REQUEST.isUnReadForAcceptUser, true);
        BasicBSONObject order = new BasicBSONObject();
        order.put(FRIEND_REQUEST.createTime, -1);
        List<FriendRequest> result = this.findAll(query, order);
        return result;
    }

    public List<FriendRequest> findUnReadBySendUser(String userId) {
        BasicBSONObject query = new BasicBSONObject();
        query.put(FRIEND_REQUEST.sendUserId, userId);
        query.put(FRIEND_REQUEST.isUnReadForSendUser, true);
        BasicBSONObject order = new BasicBSONObject();
        order.put(FRIEND_REQUEST.createTime, -1);
        List<FriendRequest> result = this.findAll(query, order);
        return result;
    }

    public void batchTagReadBySendUser(String[] requestIds) {
        if(requestIds == null || requestIds.length == 0) return;
        BasicBSONObject query = new BasicBSONObject();
        List<BasicBSONObject> matchs = new ArrayList<>();
        for (String requestId : requestIds) {
            BasicBSONObject match = new BasicBSONObject();
            match.put(FRIEND_REQUEST.friendRequestId, requestId);
            matchs.add(match);
        }
        query.put(MatchConst._OR, matchs);
        BasicBSONObject updateField = new BasicBSONObject();
        updateField.put(FRIEND_REQUEST.isUnReadForSendUser, false);
        this.update(query, updateField);
    }

    public void batchTagReadBySendUser(List<FriendRequest> requests) {
        List<String> requestIds = new ArrayList<>();
        for (FriendRequest request : requests) {
            requestIds.add(request.getFriendRequestId());
        }
        this.batchTagReadBySendUser(requestIds.toArray(new String[0]));
    }

    public void batchTagReadByAcceptUser(String[] requestIds) {
        if(requestIds == null || requestIds.length == 0) return;
        BasicBSONObject query = new BasicBSONObject();
        List<BasicBSONObject> matchs = new ArrayList<>();
        for (String requestId : requestIds) {
            BasicBSONObject match = new BasicBSONObject();
            match.put(FRIEND_REQUEST.friendRequestId, requestId);
            matchs.add(match);
        }
        query.put(MatchConst._OR, matchs);
        BasicBSONObject updateField = new BasicBSONObject();
        updateField.put(FRIEND_REQUEST.isUnReadForAcceptUser, false);


        this.update(query, updateField);
    }

    public void batchTagReadByAcceptUser(List<FriendRequest> requests) {
        List<String> requestIds = new ArrayList<>();
        for (FriendRequest request : requests) {
            requestIds.add(request.getFriendRequestId());
        }
        this.batchTagReadByAcceptUser(requestIds.toArray(new String[0]));
    }

	@Override
	protected String getDbSpaceName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String getDbCollectionName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String getPrimaryKey() {
		// TODO Auto-generated method stub
		return null;
	}


}
